package com.ztgf.shopping.api.cms.shop;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.ztgf.common.constant.Result;
import com.ztgf.common.enums.CodeEnum;
import com.ztgf.shopping.models.bo.shop.ShopInfoAuditBO;
import com.ztgf.shopping.models.dto.audit.AuditDTO;
import com.ztgf.shopping.models.dto.shop.ShopInfoAuditDTO;
import com.ztgf.shopping.models.enums.ShoppingCodeEnum;
import com.ztgf.shopping.models.service.audit.IAuditService;
import com.ztgf.shopping.models.service.shop.IShopInfoAuditService;
import com.ztgf.shopping.models.vo.shop.*;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * 店铺审核表控制器
 *
 * @author zhanghualang
 * @Date 2020-06-23 11:56:32
 */

@RestController
@RequestMapping("/cms/shopInfoAudit")
@Slf4j
@Api(value = "ShopInfoAuditController",tags = "店铺审核表接口")
public class ShopInfoAuditController {

    @DubboReference
    private IShopInfoAuditService shopInfoAuditService;
    @DubboReference
    private IAuditService iAuditService;

    @PostMapping("/getLogin")
    @ApiOperation(value = "根据id查找注册详情",notes = "根据id查找注册详情")
    @ApiImplicitParam(name = "id",value = "id",required = true,paramType = "query", dataType = "Long")
    public Result<ShopInfoAuditLoginVO> getLogin(@RequestParam("id") Long id){
        if(id == null){
            return new Result<>(CodeEnum.FAIL_100002 );
        }

        ShopInfoAuditLoginVO vo = shopInfoAuditService.getLogin(id);
        if (vo.getId() == null){
            return new Result<>(CodeEnum.FAIL);
        }else {
            return new Result<>(vo);
        }
    }

    @PostMapping("/getOpen")
    @ApiOperation(value = "根据id查找开店详情",notes = "根据id查找开店详情")
    @ApiImplicitParam(name = "id",value = "id",required = true,paramType = "query", dataType = "Long")
    public Result<ShopInfoAuditOpenVO> getOpen(@RequestParam("id") Long id){
        if(id == null){
            return new Result<>(CodeEnum.FAIL_100002 );
        }

        ShopInfoAuditOpenVO vo = shopInfoAuditService.getOpen(id);
        if (vo.getId() == null){
            return new Result<>(CodeEnum.FAIL);
        }else {
            return new Result<>(vo);
        }
    }

    @PostMapping("/getChange")
    @ApiOperation(value = "根据id查找资质变更详情",notes = "根据id查找资质变更详情")
    @ApiImplicitParam(name = "id",value = "id",required = true,paramType = "query", dataType = "Long")
    public Result<ShopInfoAuditChangeVO> getChange(@RequestParam("id") Long id){
        if(id == null){
            return new Result<>(CodeEnum.FAIL_100002 );
        }

        ShopInfoAuditChangeVO vo = shopInfoAuditService.getChange(id);
        if (vo.getId() == null){
            return new Result<>(CodeEnum.FAIL);
        }else {
            return new Result<>(vo);
        }
    }

    @PostMapping("/getBrand")
    @ApiOperation(value = "根据id查找新增品牌详情",notes = "根据id查找新增品牌详情")
    @ApiImplicitParam(name = "id",value = "id",required = true,paramType = "query", dataType = "Long")
    public Result<ShopInfoAuditNewBrandVO> getBrand(@RequestParam("id") Long id){
        if(id == null){
            return new Result<>(CodeEnum.FAIL_100002 );
        }

        ShopInfoAuditNewBrandVO vo = shopInfoAuditService.getBrand(id);
        if (vo.getId() == null){
            return new Result<>(CodeEnum.FAIL);
        }else {
            return new Result<>(vo);
        }
    }

    @PostMapping("/getCategory")
    @ApiOperation(value = "根据id查找新增类目审核详情",notes = "根据id查找新增类目审核详情")
    @ApiImplicitParam(name = "id",value = "id",required = true,paramType = "query", dataType = "Long")
    public Result<ShopInfoAuditCategoryVO> getCategory(@RequestParam("id") Long id){
        if(id == null){
            return new Result<>(CodeEnum.FAIL_100002 );
        }

        ShopInfoAuditCategoryVO vo = shopInfoAuditService.getCategory(id);
        if (vo.getId() == null){
            return new Result<>(CodeEnum.FAIL);
        }else {
            return new Result<>(vo);
        }
    }

    @ApiOperation(value = "注册待审核列表",notes = "注册待审核列表")
    @PostMapping("/queryLoginPageList")
    public Result<Page<ShopInfoAuditDTO>> queryLoginPageList(ShopInfoAuditBO bo){
        Page<ShopInfoAuditDTO> page = shopInfoAuditService.queryLoginPageList(bo);
        return new Result<>(page);
    }

    @ApiOperation(value = "注册待审核数量",notes = "注册待审核数量")
    @PostMapping("/queryLoginCount")
    public Result queryLoginCount(){
        Integer count = shopInfoAuditService.queryLoginCount();
        return new Result<>(count);
    }

    @ApiOperation(value = "开店待审核列表",notes = "开店待审核列表")
    @PostMapping("/queryOpenPageList")
    public Result<Page<ShopInfoAuditDTO>> queryOpenPageList(ShopInfoAuditBO bo){
        Wrapper<ShopInfoAuditDTO> wrapper = new EntityWrapper<>();

        if (bo.getName() != null){
            wrapper.like("name", bo.getName());
        }

        if (bo.getShopName() != null){
            wrapper.like("shop_name", bo.getShopName());
        }

        if (bo.getStartDate() != null){
            wrapper.ge("create_time", bo.getStartDate());
        }

        if (bo.getEndDate() != null){
            wrapper.le("create_time", bo.getEndDate());
        }
        wrapper.eq("type", ShoppingCodeEnum.AUDIT_TYPE_OPEN.getCode());//"审核类型 0注册，1开店，2资质变更，3新增品牌，4新增类目"
        if (bo.getStatus() != null){
            wrapper.eq("status", bo.getStatus());
        }else{
            wrapper.andNew().eq("status", ShoppingCodeEnum.AUDIT_CREATE.getCode()).or().eq("status", ShoppingCodeEnum.AUDIT_RESUBMIT.getCode());
        }

        Page<ShopInfoAuditDTO> page = new Page<>(bo.getPageNo(), bo.getPageSize());
        page = shopInfoAuditService.selectPage(page, wrapper);

        return new Result<>(page);
    }

    @ApiOperation(value = "开店待审核数量",notes = "开店待审核数量")
    @PostMapping("/queryOpenCount")
    public Result queryOpenCount(){
        Wrapper<ShopInfoAuditDTO> wrapper = new EntityWrapper<>();
        wrapper.eq("type", ShoppingCodeEnum.AUDIT_TYPE_OPEN.getCode());//"审核类型 0注册，1开店，2资质变更，3新增品牌，4新增类目"
        //0审核中，1审核通过，2审核打回，3审核不通过,4重新提交
        wrapper.andNew().eq("status", ShoppingCodeEnum.AUDIT_CREATE.getCode()).or().eq("status",ShoppingCodeEnum.AUDIT_RESUBMIT.getCode());
        Integer count = shopInfoAuditService.selectCount(wrapper);
        return new Result<>(count);
    }

    @ApiOperation(value = "资质变更待审核列表",notes = "资质变更待审核列表")
    @PostMapping("/queryChangePageList")
    public Result<Page<ShopInfoAuditDTO>> queryChangePageList(ShopInfoAuditBO bo){
        return new Result<>(shopInfoAuditService.selectChangePage(bo));
    }

    @ApiOperation(value = "资质变更待审核数量",notes = "资质变更待审核数量")
    @PostMapping("/queryChangeCount")
    public Result queryChangeCount(){
        Wrapper<ShopInfoAuditDTO> wrapper = new EntityWrapper<>();
        wrapper.eq("type", ShoppingCodeEnum.AUDIT_TYPE_CHANGE.getCode());//"审核类型 0注册，1开店，2资质变更，3新增品牌，4新增类目"
        //0审核中，1审核通过，2审核打回，3审核不通过,4重新提交
        wrapper.eq("status", ShoppingCodeEnum.AUDIT_CREATE.getCode());
        Integer count = shopInfoAuditService.selectCount(wrapper);
        return new Result<>(count);
    }

    @ApiOperation(value = "品牌新增待审核列表",notes = "品牌新增待审核列表")
    @PostMapping("/queryAddBrandPageList")
    public Result<Page<ShopInfoAuditDTO>> queryAddBrandPageList(ShopInfoAuditBO bo){
        return new Result<>(shopInfoAuditService.selectAddBrandPage(bo));
    }

    @ApiOperation(value = "品牌新增待审核数量",notes = "品牌新增待审核数量")
    @PostMapping("/queryAddBrandCount")
    public Result queryAddBrandCount(){
        Wrapper<ShopInfoAuditDTO> wrapper = new EntityWrapper<>();
        wrapper.eq("type", ShoppingCodeEnum.AUDIT_TYPE_INSERTBRAND.getCode());//"审核类型 0注册，1开店，2资质变更，3新增品牌，4新增类目"
        //0审核中，1审核通过，2审核打回，3审核不通过,4重新提交
        wrapper.eq("status", ShoppingCodeEnum.AUDIT_CREATE.getCode());
        Integer count = shopInfoAuditService.selectCount(wrapper);
        return new Result<>(count);
    }

    @ApiOperation(value = "新增品牌类目待审核列表",notes = "新增品牌类目待审核列表")
    @PostMapping("/queryAddCategoryPageList")
    public Result<Page<ShopInfoAuditDTO>> queryAddCategoryPageList(ShopInfoAuditBO bo){
        return new Result<>(shopInfoAuditService.selectAddCategoryPage(bo));
    }

    @ApiOperation(value = "新增品牌类目待审核数量",notes = "新增品牌类目待审核数量")
    @PostMapping("/queryAddCategoryCount")
    public Result queryAddCategoryCount(){
        Wrapper<ShopInfoAuditDTO> wrapper = new EntityWrapper<>();
        wrapper.eq("type", ShoppingCodeEnum.AUDIT_TYPE_INSERTCATEGORY.getCode());//"审核类型 0注册，1开店，2资质变更，3新增品牌，4新增类目"
        //0审核中，1审核通过，2审核打回，3审核不通过,4重新提交
        wrapper.eq("status", ShoppingCodeEnum.AUDIT_CREATE.getCode());
        Integer count = shopInfoAuditService.selectCount(wrapper);
        return new Result<>(count);
    }

    @ApiOperation(value = "所有待审核数量",notes = "所有待审核数量")
    @PostMapping("/queryCount")
    public Result queryCount(){
        Wrapper<ShopInfoAuditDTO> wrapper = new EntityWrapper<>();
        //0审核中，1审核通过，2审核打回，3审核不通过,4重新提交
        wrapper.eq("status", ShoppingCodeEnum.AUDIT_CREATE.getCode()).or().eq("status", ShoppingCodeEnum.AUDIT_RESUBMIT.getCode());
        Integer count = shopInfoAuditService.selectCount(wrapper);
        return new Result<>(count);
    }
}
